package com.fancy.learncenter.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonErrorUnitListBean;
import com.fancy.learncenter.bean.CartoonErrorUnitListItemBean;
import com.fancy.learncenter.bean.CartoonUnitBean;
import com.fancy.learncenter.bean.CartoonUnitListBean;
import com.fancy.learncenter.common.JumpIntentKey;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.CartoonErrorUnitListAdapter;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class CartoonErrorUnitListActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.cartoon_rope)
    ImageView cartoonRope;
    @Bind(R.id.finished_work)
    ImageView finishedWork;
    @Bind(R.id.finished_work_num)
    TextView finishedWorkNum;
    @Bind(R.id.not_correction_head_layout)
    LinearLayout notCorrectionHeadLayout;
    @Bind(R.id.has_corrected_icon_image)
    ImageView hasCorrectedIconImage;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    private String unCorrectNums;
    private JumpIntentKey.QUESTION_ENTER_TYPE type;
    private List<CartoonErrorUnitListItemBean> unitList;
    private CartoonErrorUnitListAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setCartoonContentView(R.layout.activity_cartoon_error_unit_list, Status.STATUS);
        ButterKnife.bind(this);
        getIntentData();
        initView();
        initRecycleView();
        getErrorUnitList();
    }

    private void getIntentData(){

        type = (JumpIntentKey.QUESTION_ENTER_TYPE) getIntent().getSerializableExtra(JumpIntentKey.QUESTION_ENTER);
        unCorrectNums = getIntent().getStringExtra(JumpIntentKey.NOT_CORRECTED_NUM);
    }

    private void initView(){

        switch (type){
            case NOT_CORRECTED:
                notCorrectionHeadLayout.setVisibility(View.VISIBLE);
                hasCorrectedIconImage.setVisibility(View.GONE);
                break;
            case HAS_CORRECTED:
                notCorrectionHeadLayout.setVisibility(View.GONE);
                hasCorrectedIconImage.setVisibility(View.VISIBLE);
                hasCorrectedIconImage.setImageResource(R.mipmap.cartoon_has_corrected_icon);
                break;
            case ERROR_KINDS:
                notCorrectionHeadLayout.setVisibility(View.GONE);
                hasCorrectedIconImage.setVisibility(View.VISIBLE);
                hasCorrectedIconImage.setImageResource(R.mipmap.cartoon_error_kinds_head_icon);
                break;
        }

        if (!TextUtils.isEmpty(unCorrectNums)) {
            finishedWorkNum.setText("("+unCorrectNums+")");
        }
    }

    private void initRecycleView(){
        unitList = new ArrayList<>();
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new CartoonErrorUnitListAdapter(this,unitList,type);
        recyclerView.setAdapter(adapter);



    }

    private void getErrorUnitList(){

        Map<String,Object> map = new HashMap<>();
        map.put("classId",MyApplication.classIDStr);
        switch (type) {
            case NOT_CORRECTED:
            case ERROR_KINDS:
            map.put("queryType", 1);
            break;
            case HAS_CORRECTED:
                map.put("queryType", 2);
                break;
        }
        HttpMehtod.getInstance().cartoonListHomeworkError(map, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<CartoonErrorUnitListBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonErrorUnitListBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(),baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonErrorUnitListBean>() {
                    @Override
                    public void success(CartoonErrorUnitListBean dataBean) {

                        if (dataBean != null) {
                            unitList.addAll(dataBean.getResult());
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();

            }
        });

    }


    /**
     * 获取单元列表
     */
//    private void getUnitList(){
//
//        Map<String,Object> map = new HashMap<>();
//        map.put("classId", MyApplication.classIDStr);
//        HttpMehtod.getInstance().cartoonGetUnitList(map, new CustomNetSubscriber(this) {
//            @Override
//            public void onNext(Response<ResponseBody> responseBodyResponse) {
//
//                BaseBean<CartoonUnitListBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonUnitListBean.class);
//                CustomNetCallBack callBack = new CustomNetCallBack(getContext(),baseBean);
//                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonUnitListBean>() {
//                    @Override
//                    public void success(CartoonUnitListBean dataBean) {
//
//                        if (dataBean != null) {
//                            unitList.addAll(dataBean.getResult());
//                            adapter.notifyDataSetChanged();
//                        }
//                    }
//
//                    @Override
//                    public void fail(String errorCode) {
//
//                    }
//                }).dealData();
//            }
//        });
//
//    }



}
